﻿using System;
using System.Collections.Generic;
using EPiAbstractions.Common.Categories;
using EPiServer.Common.Categories;
using EPiServer.Community.Blog;
using JoelAbrahamsson.MetaWeblog;
using Category=EPiServer.Common.Categories.Category;

namespace JoelAbrahamsson.BlogExtensions
{
    public class BlogCategoryProvider : IBlogCategoryProvider
    {
        public const string BlogCategoriesRootName = "Blog Categories";
        public const string CategoryNameDelimiter = "/";

        public BlogCategoryProvider(ICategoryHandlerFacade categoryHandlerFacade)
        {
            CategoryHandlerFacade = categoryHandlerFacade;
        }

        public virtual CategoryInfo[] GetBlogCategories(Blog blog)
        {
            List<CategoryInfo> categories = new List<CategoryInfo>();
            CategoryCollection childrenOfRoot = CategoryHandlerFacade.GetCategories(BlogCategoriesRoot);
            foreach (ICategory child in childrenOfRoot)
            {
                CategoryCollection grandChildren = CategoryHandlerFacade.GetCategories(child);
                foreach (ICategory grandChild in grandChildren)
                {
                    categories.Add(CreateCategoryInfo(grandChild));
                }
            }
            return categories.ToArray();
        }

        public virtual CategoryInfo CreateCategoryInfo(ICategory category)
        {
            return new CategoryInfo
                       {
                           categoryid = category.ID.ToString(), 
                           title = CreateCategoryInfoTitle(category),
                           description = string.Empty,
                           htmlUrl = string.Empty,
                           rssUrl = string.Empty
                       };
        }

        protected virtual string CreateCategoryInfoTitle(ICategory category)
        {
            return category.Parent.Name + CategoryNameDelimiter + category.Name;
        }

        public virtual ICategory BlogCategoriesRoot
        {
            get
            {
                ICategory category = CategoryHandlerFacade.GetCategory(BlogCategoriesRootName);

                if (category != null)
                    return category;

                category = new Category(BlogCategoriesRootName);
                return CategoryHandlerFacade.AddCategory(category);
            }  
        }

        public ICategoryHandlerFacade CategoryHandlerFacade { get; set; }


        public ICategory ParseCategory(string categoryInfoTitle)
        {
            string[] titleParts = categoryInfoTitle.Split(new [] { CategoryNameDelimiter }, StringSplitOptions.None);
            if (titleParts.Length != 2)
                return null;

            string categoryPath = BlogCategoriesRootName
                + CategoryNameDelimiter
                + titleParts[0]
                + CategoryNameDelimiter
                + titleParts[1];

            return CategoryHandlerFacade.GetCategory(categoryPath);
        }
    }
}
